import { MessageHead } from "./MessageHead";
import { SerializeStream } from "../Serialize/SerializeStream";
import { MessageManager } from "./MessageManager";
import { IMessageEx } from "./IMessageEx";
import { IMessageBase } from "./IMessageBase";
//class MessageBlock

export class MessageBlock {
  public __write(arg1: SerializeStream): void {
    this.__writeHead(arg1);
    this.__writeBody(arg1);
  }

  public __writeHead(arg1: SerializeStream): void {
    this._messageHead.__write(arg1);
  }

  public __writeBody(arg1: SerializeStream): void {
    if (this._messageBase) {
      arg1.writeSize(this._messageBase.getType());
      this._messageBase.__write(arg1);
      if (this._messageBase.msgEx) {
        arg1.writeByte(1);
        this._messageBase.msgEx.__write(arg1);
      } else {
        arg1.writeByte(0);
      }
    } else {
      arg1.writeSize(0);
    }
  }

  public __read(arg1: SerializeStream): void {
    this.__readHead(arg1);
    this.__readBody(arg1);
  }

  public __readHead(arg1: SerializeStream): void {
    this._messageHead.__read(arg1);
  }

  public __readBody(arg1: SerializeStream): void {
    var loc2 = 0;
    var size = arg1.readSize();
    if (size !== 0) {
      this._messageBase = MessageManager.instance().createMessage(size)!;
      this._messageBase.__read(arg1);
      loc2 = arg1.readByte();
      if (loc2 != 0) {
        this._messageBase.msgEx = new IMessageEx();
        this._messageBase.msgEx.__read(arg1);
      }
    }
  }

  public get messageHead(): MessageHead {
    return this._messageHead;
  }

  public set messageHead(arg1: MessageHead) {
    this._messageHead = arg1;
  }

  public get messageBase(): IMessageBase {
    return this._messageBase;
  }

  public set messageBase(arg1: IMessageBase) {
    this._messageBase = arg1;
  }

  private _messageHead: MessageHead = new MessageHead();

  private _messageBase!: IMessageBase;
}
